//
//  HomeOptionViewController.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/27.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "HomeOptionViewController.h"
#import "HomeOptionDataShared.h"
#import "HomeOptionCollectionViewCell.h"
#import "CommodityViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface HomeOptionViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, copy) NSArray *dataArr;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation HomeOptionViewController

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
//    [self initNavTitle];
    [self initCollectionView];
    [self getNetData];
}

#pragma mark 初始化导航
//- (void)initNavTitle {
//    self.navigationItem.title = self.title;
//    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
//}
//- (void)backClick {
//    [self.navigationController popViewControllerAnimated:YES];
//}

#pragma mark  初始化网格
- (void)initCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH / 2 - 15, HEIGHT / 2.5);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,WIDTH,HEIGHT-64) collectionViewLayout:layout];
    collection.delegate = self;
    collection.dataSource = self;
    collection.backgroundColor = [UIColor whiteColor];
    [collection registerClass:[HomeOptionCollectionViewCell class] forCellWithReuseIdentifier:@"HomeOptionCollectionViewCell"];
    [self.view addSubview:collection];
    _collectionView = collection;
}

#pragma mark 获取数据
- (void)getNetData {
    [[HomeOptionDataShared sharedData] getURL:self.url data:^(NSArray *array) {
        _dataArr = array;
        [self.collectionView reloadData];
    }];
}

#pragma mark - UICollectionViewDataSource 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeOptionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeOptionCollectionViewCell" forIndexPath:indexPath];
    cell.model = _dataArr[indexPath.row];
    cell.layer.cornerRadius = 3;
    cell.layer.masksToBounds = YES;
    cell.layer.borderWidth = 0.2;
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CommodityViewController *comVC = [[CommodityViewController alloc] init];
    HomeOptionModel *model = _dataArr[indexPath.row];
    comVC.sid = model.sID;
    [self.navigationController pushViewController:comVC animated:YES];
}

@end
